﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace YArchitech.Public.Forms
{
	public class ColorPickerCell : DataGridViewTextBoxCell
	{
		public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
		{
			base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
			ColorPickerControl colorPickerControl = base.DataGridView.EditingControl as ColorPickerControl;
			this._ColorPicker = colorPickerControl;
			if (base.Value != null && base.Value.GetType() == typeof(Color))
			{
				colorPickerControl.Color = (Color)base.Value;
			}
		}

		public override Type EditType
		{
			get
			{
				return typeof(ColorPickerControl);
			}
		}

		public override Type ValueType
		{
			get
			{
				return typeof(Color);
			}
		}

		public override object DefaultNewRowValue
		{
			get
			{
				return System.Drawing.Color.White;
			}
		}

		protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
		{
			formattedValue = null;
			base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
			Rectangle rect = default(Rectangle);
			RectangleF layoutRectangle = default(RectangleF);
			this.GetDisplayLayout(cellBounds, ref rect, ref layoutRectangle);
			if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
			{
				SolidBrush solidBrush;
				if (value != null && value.GetType() == typeof(Color))
				{
					solidBrush = new SolidBrush((Color)value);
				}
				else
				{
					solidBrush = new SolidBrush(cellStyle.BackColor);
				}
				graphics.FillRectangle(solidBrush, rect);
				graphics.DrawRectangle(Pens.Black, rect);
				graphics.DrawString(((Color)value).Name.ToString(), cellStyle.Font, Brushes.Black, layoutRectangle);
				solidBrush.Dispose();
			}
		}

		public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
		{
			"0x" + formattedValue.ToString();
			int num;
			if (int.TryParse(formattedValue.ToString(), NumberStyles.HexNumber, null, out num))
			{
				return base.ParseFormattedValue("0x" + formattedValue.ToString(), cellStyle, formattedValueTypeConverter, valueTypeConverter);
			}
			return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
		}

		protected virtual void GetDisplayLayout(Rectangle CellRect, ref Rectangle colorBoxRect, ref RectangleF textBoxRect)
		{
			colorBoxRect.X = CellRect.X + 2;
			colorBoxRect.Y = CellRect.Y + 1;
			colorBoxRect.Size = new Size(25, CellRect.Height - 4);
			textBoxRect = RectangleF.FromLTRB((float)(colorBoxRect.X + colorBoxRect.Width + 5), (float)(colorBoxRect.Y + 2), (float)(CellRect.X + CellRect.Width - 2), (float)(colorBoxRect.Y + colorBoxRect.Height));
		}

		private ColorPickerControl _ColorPicker;
	}
}
